package net.lunjin.service.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.math.BigDecimal;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.lunjin.bean.News;
import net.lunjin.bean.NewsKey;
import net.lunjin.dao.NewsDao;
import net.lunjin.service.NewsService;
import net.lunjin.util.Constants;
import net.lunjin.util.ProjectUtil;

import org.springframework.beans.factory.annotation.Autowired;

import finaler.util.Page;
import finaler.util.PageList;

public class NewsServiceImpl implements NewsService {

	@Autowired
	private NewsDao newsDao;
	
	@Override
	public boolean insert(News news) throws Exception {
		return newsDao.insert(news);
	}

	@Override
	public void delete(News news) throws Exception {
		newsDao.delete(news);
	}
	@Override
	public PageList getNewsList(Page page) throws Exception {
		return newsDao.getNewsList(page);
	}

	@Override
	public News findByKey(BigDecimal newsId) throws Exception {
		return newsDao.findByKey(new NewsKey(newsId));
	}

	@Override
	public boolean update(News news) throws Exception {
		return newsDao.update(news);
	}

	@Override
	public List<News> getTopRecord(int newsType, int limit) throws Exception {
		return newsDao.getTopRecord(newsType, limit);
	}

	@Override
	public List<News> getHotRecord(int newsType, int limit) throws Exception {
		return newsDao.getHotRecord(newsType, limit);
	}
	
	@Override
	public List<News> getRandomRecord(int newsType, int limit) throws Exception {
		return newsDao.getRandomRecord(newsType, limit);
	}

	@Override
	public List<News> getBbsTopRecord(int limit) throws Exception {
		return newsDao.getBbsTopRecord(limit);
	}

	@Override
	public PageList getBbsList(Page page) throws Exception {
		return newsDao.getBbsList(page);
	}

	@Override
	public List<News> getBeforeAfterList(BigDecimal newsId, BigDecimal newsType) throws Exception {
		return newsDao.getBeforeAfterList(newsId,newsType);
	}

	@Override
	public PageList findAll(Page page) throws Exception {
		return newsDao.getAll(page);
	}

	@Override
	public void autoUpdateHit() throws Exception {
		newsDao.autoUpdateHit();
	}

	@Override
	public void autoCreateNewsPage(String file_real_path) throws Exception {
		OutputStreamWriter out = null;
		OutputStreamWriter out2 = null;
		String jspFileNameString = "qqgoldnews.jsp";
		String webContentString = ProjectUtil.getWebSource("http://finance.qq.com/l/gold/hjzx/index.htm","gb2312");
		String rn = System.getProperty("line.separator");//System.getProperty("line.separator")的意思是 "\r\n"
		String headString  = "<%@ page contentType=\"text/html; charset=UTF-8\" %>" + rn;

		String reg = "<div class=\"mod newslist\"><ul><li>(.+)</li></ul></div>";
		Pattern pt = Pattern.compile(reg);
		Matcher mc= pt.matcher(webContentString); 
		if(mc.find()){
			String newsString  = mc.group(1).replaceAll("href=\"", "class=\"title\" href=\""+Constants.URL+"/news/jynewsDetail_");
			newsString  = newsString.replaceAll("</ul><ul>","");
			newsString  = newsString.replaceAll("http://finance.qq.com/a/","");
			try {
				//File.separator是跨平台的路径符
				out = new OutputStreamWriter(new FileOutputStream(file_real_path + File.separator + jspFileNameString ),"UTF-8" );
				out.write( headString);
				out.write( "<ul class=\"e2\"><li>");
				out.write( newsString );
				out.write( "</li></ul>");
				
				//组装滚动新闻
				String scrollString = "<font size=\"3\">";
				jspFileNameString = "jynewsScroll.jsp";
				String[] hrefList = newsString.split("pub_time");
				for(int i=0;i<11;i++){
					Pattern pt2 = Pattern.compile("<li>(.+)<span");
					Matcher mc2 = pt2.matcher(hrefList[i]); 
					if(mc2.find()){
						scrollString+=mc2.group(1);
					}
				}
				scrollString+="</font>";
				out2 = new OutputStreamWriter(new FileOutputStream(file_real_path + File.separator + jspFileNameString ),"UTF-8" );
				out2.write( headString);
				out2.write( scrollString );
				//滚动新闻结束
			} catch (Exception e) {
				if(Constants.ISDEBUG){
					e.printStackTrace();
				}
			}finally{
				if (out != null) {
	                try {
	                	out.flush();
	                	out.close();
	                } catch (IOException ioe) {
	    				if(Constants.ISDEBUG){
	    					ioe.printStackTrace();
	    				}
	                }
	        	}
				if (out2 != null) {
	                try {
	                	out2.flush();
	                	out2.close();
	                } catch (IOException ioe) {
	    				if(Constants.ISDEBUG){
	    					ioe.printStackTrace();
	    				}
	                }
	        	}
			}
		}
	}
	
	@Override
	public void autoCreateCalendarPage(String file_real_path) throws Exception {
		OutputStreamWriter out = null;
		String rn = System.getProperty("line.separator");//System.getProperty("line.separator")的意思是 "\r\n"
		String headString  = "<%@ page contentType=\"text/html; charset=UTF-8\" %>" + rn;
		
		String reg = "<div class=\"TableBlock\" id=\"div_currency\">(.+)<div class=\"con_sm\">";
		for(int i=0;i<8;i++){//只抓一个星期的财经日历(从昨天开始累加8天)
			String dayString = ProjectUtil.getDayOper("yyyyMMdd",i-1);
			String webContentString = ProjectUtil.getWebSource("http://www.fx678.com/indexs/html/"+dayString+".shtml","gb2312");
			Pattern pt = Pattern.compile(reg);
			Matcher mc= pt.matcher(webContentString);
			if(mc.find()){
				try {
					String newsString  = mc.group(1).replaceAll("<a", "<noa")
							.replaceAll("<img", "<noimg")
							.replaceAll("<tr><td>", "<tr><td><img src=\""+Constants.URL+"/images/calendar.jpg\" alt=\"pic\"/>&nbsp;")
							.replaceAll("图表", "其它")
							.replaceAll("解读", "备注")
							.replaceAll("http://www.fx678.com", "###")
							.replaceAll("725px", "900px")
							.replaceAll("16px;\" >", "16px;\" ><tr id=\"scrolldiv\" stype=\"display:none\"><th width=\"53px\">时间</th><th width=\"169px\">国家</th><th width=\"233px\" style=\"padding: 0 9px;\">指标名称</th><th width=\"52\">重要性</th><th width=\"74\">前值</th><th width=\"75\">预测值</th><th width=\"75\">公布值</th><th width=\"45px\">其它</th><th width=\"94px\">备注</th></tr>")
							.replaceAll("<th>                                    国家", "<th width=\"169px\">             国家")
							//.replaceAll("</td><td>", "</td><td width=\"80px\">")
							//.replaceAll("<tr><td>", "<tr><td width=\"53px\">") 
							;
					String jspFileNameString = i + ".jsp";
					//File.separator是跨平台的路径符
					out = new OutputStreamWriter(new FileOutputStream(file_real_path + File.separator + jspFileNameString ),"UTF-8" );
					out.write( headString);
					out.write( newsString );
				} catch (Exception e) {
					if(Constants.ISDEBUG){
						e.printStackTrace();
					}
				}finally{
					if (out != null) {
		                try {
		                	out.flush();
		                	out.close();
		                } catch (IOException ioe) {
		    				if(Constants.ISDEBUG){
		    					ioe.printStackTrace();
		    				}
		                }
		        	}
				}
			}
		}
	}
	
	@Override
	public News getByBaiduId(String baiduid) throws Exception {
		return newsDao.getByBaiduId(baiduid);
	}

	@Override
	public News findByTitle(String title,int newsType) throws Exception {
		return newsDao.findByTitle(title,newsType);
	}

}
